package com.engine.qz.controller;

import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.engine.common.util.ParamUtil;
import com.engine.common.util.ServiceUtil;

import com.engine.qz.service.HrmCustomTemplateService;


import com.engine.qz.service.impl.HrmCustomTemplateServiceImpl;
import weaver.hrm.HrmUserVarify;
import weaver.hrm.User;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.ws.rs.*;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
import java.util.HashMap;
import java.util.Map;

/**
 * @Author Apple
 * @Date 2025/2/24 11:07
 * @Description: 钱智定制化花名册
 * @Version 1.0
 */
public class HrmCustomTemplateAction {

    private HrmCustomTemplateService getService(User user) {
        return ServiceUtil.getService(HrmCustomTemplateServiceImpl.class,user);
    }


    @GET
    @Path("/getCustomTransferData")
    @Produces(MediaType.TEXT_PLAIN)
    public String getCustomTransferData(@Context HttpServletRequest request, @Context HttpServletResponse response){
        Map<String, Object> data = new HashMap<>(8);
        try {
            User user = HrmUserVarify.getUser(request, response);
            Map<String, Object> map = ParamUtil.request2Map(request);
            data.put("result",getService(user).getCustomTransferData(map));
            data.put("api_status", true);
        } catch (Exception e) {
            data.put("api_status", false);
            data.put("msg", "catch exception : " + e.getMessage());
        }
        return JSONObject.toJSONString(data, SerializerFeature.DisableCircularReferenceDetect);
    }


    @GET
    @Path("/getCustomTemplate")
    @Produces(MediaType.TEXT_PLAIN)
    public String getCustomTemplate(@Context HttpServletRequest request, @Context HttpServletResponse response){
        Map<String, Object> data = new HashMap<>(8);
        try {
            User user = HrmUserVarify.getUser(request, response);
            data.put("result",getService(user).getCustomTemplate());
            data.put("api_status", true);
        } catch (Exception e) {
            data.put("api_status", false);
            data.put("msg", "catch exception : " + e.getMessage());
        }
        return JSONObject.toJSONString(data, SerializerFeature.DisableCircularReferenceDetect);
    }

    @GET
    @Path("/getTemplateSelectKeys")
    @Produces(MediaType.TEXT_PLAIN)
    public String getTemplateSelectKeys(@Context HttpServletRequest request, @Context HttpServletResponse response,
                                        @QueryParam("templateId") String templateId){
        Map<String, Object> data = new HashMap<>(8);
        try {
            User user = HrmUserVarify.getUser(request, response);
            data.put("result",getService(user).getTemplateSelectKeys(templateId));
            data.put("api_status", true);
        } catch (Exception e) {
            data.put("api_status", false);
            data.put("msg", "catch exception : " + e.getMessage());
        }
        return JSONObject.toJSONString(data, SerializerFeature.DisableCircularReferenceDetect);
    }


    @POST
    @Path("/addCustomTemplate")
    @Produces(MediaType.TEXT_PLAIN)
    public String addCustomTemplate(@Context HttpServletRequest request, @Context HttpServletResponse response){
        Map<String, Object> data = new HashMap<>(8);
        try {
            User user = HrmUserVarify.getUser(request, response);
            Map<String, Object> map = ParamUtil.request2Map(request);
            data.putAll(getService(user).addCustomTemplate(map));
            data.put("api_status", true);
        } catch (Exception e) {
            data.put("api_status", false);
            data.put("msg", "catch exception : " + e.getMessage());
        }
        return JSONObject.toJSONString(data, SerializerFeature.DisableCircularReferenceDetect);
    }

    @POST
    @Path("/updateCustomTemplateFields")
    @Produces(MediaType.TEXT_PLAIN)
    public String updateCustomTemplateFields(@Context HttpServletRequest request, @Context HttpServletResponse response){
        Map<String, Object> data = new HashMap<>(8);
        try {
            User user = HrmUserVarify.getUser(request, response);
            Map<String, Object> map = ParamUtil.request2Map(request);
            getService(user).updateCustomTemplateFields(map);
            data.put("api_status", true);
        } catch (Exception e) {
            data.put("api_status", false);
            data.put("msg", "catch exception : " + e.getMessage());
        }
        return JSONObject.toJSONString(data, SerializerFeature.DisableCircularReferenceDetect);
    }

    @GET
    @Path("/customTemplateTable")
    @Produces(MediaType.TEXT_PLAIN)
    public String customTemplateTable(@Context HttpServletRequest request, @Context HttpServletResponse response){
        Map<String, Object> data = new HashMap<>(8);
        try {
            User user = HrmUserVarify.getUser(request, response);
            data.put("result",getService(user).customTemplateTable());
            data.put("api_status", true);
        } catch (Exception e) {
            data.put("api_status", false);
            data.put("msg", "catch exception : " + e.getMessage());
        }
        return JSONObject.toJSONString(data, SerializerFeature.DisableCircularReferenceDetect);
    }


    @POST
    @Path("/saveCustomTemplateTable")
    @Produces(MediaType.TEXT_PLAIN)
    public String saveCustomTemplateTable(@Context HttpServletRequest request, @Context HttpServletResponse response){
        Map<String, Object> data = new HashMap<>(8);
        try {
            User user = HrmUserVarify.getUser(request, response);
            Map<String, Object> map = ParamUtil.request2Map(request);
            getService(user).saveCustomTemplateTable(map);
            data.put("api_status", true);
        } catch (Exception e) {
            data.put("api_status", false);
            data.put("msg", "catch exception : " + e.getMessage());
        }
        return JSONObject.toJSONString(data, SerializerFeature.DisableCircularReferenceDetect);
    }

}
